package com.xsg.printer.core.toolkit;

import com.xsg.printer.core.model.XsgCoordinate;

import java.awt.*;
import java.awt.geom.QuadCurve2D;

public class ImageLinePaint {

    /**
     * 实线绘制函数
     * @return
     */
    public static DrawLine<Graphics2D> getFullDraw() {
        return (container, start, end) -> {
            container.setStroke(getFullLineStroke());
            container.drawLine(start.getX(), start.getY(), end.getX(), end.getY());
        };
    }

    /**
     * 虚线绘制函数
     * @return
     */
    public static DrawLine<Graphics2D> getDashedDraw() {
        return (container, start, end) -> {
            container.setStroke(getDashedLineStroke());
            container.drawLine(start.getX(), start.getY(), end.getX(), end.getY());
        };
    }


    private static int waveWidth = 10;
    private static int waveHeight = 5;
    /**
     * 波浪线绘制函数
     * @return
     */
    public static DrawLine<Graphics2D> getWaveDraw() {
        return (container, start, end) -> {
            //todo...优化
            QuadCurve2D.Double curve = new QuadCurve2D.Double();
            double limitWidth = end.getX() - start.getX();
            double limitHeight = end.getY() - end.getY();
            double pour = Math.sqrt(limitHeight*limitHeight + limitWidth*limitWidth);
            double times = pour / waveWidth;
            double x1 = start.getX();
            double y1 = start.getY();
            double x2;
            double y2;
            double ctrlx;
            double ctrly;
            int pourWaveWidth = waveWidth;
            for (double i = 1; i <= times; i++) {
                x2 = start.getX() + limitWidth*(pourWaveWidth/pour);
                y2 = start.getY() + limitHeight*(pourWaveWidth/pour);
                ctrlx = x2 + waveWidth/2;
                ctrly = y2 - waveHeight;
                curve.setCurve(x1, y1, ctrlx, ctrly, x2, y2);
                container.draw(curve);
                pourWaveWidth += waveWidth;
                x1 = x2;
                y1 = y2;
            }
        };
    }

    public static BasicStroke getFullLineStroke() {
        return new BasicStroke(1.5f);
    }

    public static BasicStroke getDashedLineStroke() {
        return new BasicStroke(1.5f, BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND, 2.5f, new float[]{10,5}, 0f);
    }

    public interface DrawLine<T> {
        void draw(T t, XsgCoordinate start, XsgCoordinate end);
    }
}
